import urllib, urllib2
#import django.utils.simplejson as jsondata
from django.http import HttpResponse, HttpResponseRedirect
from activitystream.lib.xml2dict import fromstring
from conf.SlideShareConf import SlideShareConf
import sys
class SlideData:

    def processUrl(self,url):
        try:
            f = urllib2.urlopen(url)
            #json = f.read()
            json = fromstring(f.read())
            #return HttpResponse('Video title: %s' % json)
            return json
        except:
            #return '[{"text":"Looks like Twitter is down. Thanks for visiting us. Please try again latter."}]'
            return sys.exc_info()[1]
            raise

    def getUserName(self):
        conf = SlideShareConf()
        userName = conf.getusername()
        return userName
    
    def getTags(self):    
        conf = SlideShareConf()    
        params = conf.getParamList("","")   
        
        url = 'http://www.slideshare.net/api/2/get_user_tags?%s'%params    
        #return HttpResponse('Url: %s' % url)
        json = self.processUrl(url)
        return json

    def getUserSlides(self):
        conf = SlideShareConf()    
        user_name = conf.getusername()
        params = conf.getParamList('username_for', user_name)
        
        url = 'http://www.slideshare.net/api/2/get_slideshows_by_user?%s'%params
        json = self.processUrl(url)        
        return json  
    
    def getSlidesByTag(self,tagName):
        conf = SlideShareConf()          
        params = conf.getParamList('tag', tagName)        
        url = 'http://www.slideshare.net/api/2/get_slideshows_by_tag?%s'%params
        return HttpResponse('Url: %s' % url)
        json = self.processUrl(url)        
        return json  